; ======================================================================
; boosta
; Created by Patrick Marum
; Date : 2009/03/19
; Funny and small racing 3d game 
;
; 
;Copyright (c) 2009 <Patrick Marum as patmaba>

;Permission is hereby granted, free of charge, To any person
;obtaining a copy of this software And associated documentation
;files (the "Software"), To deal in the Software without
;restriction, including without limitation the rights To use,
;copy, modify, merge, publish, distribute, sublicense, And/Or sell
;copies of the Software, And To permit persons To whom the
;Software is furnished To do so, subject To the following
;conditions:

;The above copyright notice And this permission notice shall be
;included in all copies Or substantial portions of the Software.

;THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
;EXPRESS Or IMPLIED, INCLUDING BUT Not LIMITED To THE WARRANTIES
;OF MERCHANTABILITY, FITNESS For A PARTICULAR PURPOSE And
;NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS Or COPYRIGHT
;HOLDERS BE LIABLE For ANY CLAIM, DAMAGES Or OTHER LIABILITY,
;WHETHER IN AN ACTION OF CONTRACT, TORT Or OTHERWISE, ARISING
;FROM, OUT OF Or IN CONNECTION WITH THE SOFTWARE Or THE USE Or
;OTHER DEALINGS IN THE SOFTWARE.
; ======================================================================

Include "lib\Inc_Const.bb"
Include "lib\Inc_TChrono.bb"
Include "lib\Inc_Timer.bb"
Include "lib\Inc_TWaypoint.bb"
Include "lib\Inc_TWheel.bb"
Include "lib\Inc_TWall.bb"
Include "lib\Inc_TCar.bb"
Include "lib\Inc_TRaceData.bb"
Include "lib\Inc_Util.bb"
Include "lib\vector.bb"
Include "lib\Inc_TList.bb"
Include "lib\Inc_TMap.bb"
Include "lib\Inc_Item.bb"
Include "lib\Inc_Explosion.bb"
Include "lib\Inc_Los.bb"


Type TApplication
	Field debug
	Field MDL%
	Field TEMPLATE_MISSIL
	Field Cam1%
	Field Light1%
	Field Plane1%
	Field Player.TCar
	Field RaceData.TRaceData
	
	Field chrono.TChrono
	
	Field gameFPS
	Field framePeriod
	Field frameTime
	Field frameElapsed
    Field frameTween#	
	Field frameTicks
	Field frameLimit
	
	Field Race = False 
	
	Field tm1.Timer = Null ; 1sec
	Field tm2.Timer = Null ; 2sec
	Field tm3.Timer = Null ; 3sec
	
	Field fntArial50B =0
	Field fntArial15B =0
	
	Field UserCamMode = 0
	
	Field gMapWayPt.TMap= Null
End Type 

; g_App is the only instance of a TApplication type 
; and the only global variable on this application.
Global g_App.TApplication = Null

; --------------------------------------------------
; Init application
; --------------------------------------------------
App_Init_Scene() 

; --------------------------------------------------
; Main loop using tweening 
; --------------------------------------------------
Repeat 
	
	Repeat 
		g_App\frameElapsed = MilliSecs () - g_App\frameTime 
	Until g_App\frameElapsed 
	
	g_App\frameTicks = g_App\frameElapsed / g_App\framePeriod 
	
	g_App\frameTween# = Float (g_App\frameElapsed Mod g_App\framePeriod) / Float (g_App\framePeriod) 
	
	For g_App\frameLimit = 1 To g_App\frameTicks 
		
		If g_App\frameLimit = g_App\frameTicks Then CaptureWorld 
		g_App\frameTime = g_App\frameTime + g_App\framePeriod 
		
		
		; --------------------------------------------------
		; Update application process
		; --------------------------------------------------
    	App_Pre_Update() 
		UpdateWorld ; update collision system
		App_Post_Update()
	Next 
	
	RenderWorld g_App\frameTween 
	
	; --------------------------------------------------
	; Display 2d application 
	; --------------------------------------------------	
	App_Update2D()
	Flip() 
	
Until KeyDown(1) 

; --------------------------------------------------
;Clear all resources and end application
; --------------------------------------------------
App_Release()
End() 


; --------------------------------------------------
; App_Release
; Clear all ressources used by application
; --------------------------------------------------
Function App_Release()
	ClearCollisions 
	FreeFont g_App\fntArial15B
	FreeFont g_App\fntArial50B
	ClearWorld 
	EndGraphics() 
End Function 
End() 



; --------------------------------------------------
; App_Init_Scene
; Init 3d world and rendering settings
; --------------------------------------------------
Function App_Init_Scene()  
;	Graphics3D 800/3,600/3,16,2  	
	Graphics3D 1024,800,16,2  	
	
	SetBuffer BackBuffer() 
	SeedRnd MilliSecs() 	
	
	
	; You must have only one instance of Tapplication
	Delete Each TApplication
	g_App = New TApplication
	g_App\gameFPS = 50
	g_App\framePeriod=0
	g_App\frameTime=0
	
	g_App\Player.TCar=	Null   
	g_App\RaceData.TRaceData = Null
	g_App\UserCamMode = 2
	g_App\debug      = 	False
	g_App\MDL%		=	0 
	g_App\Cam1%		=	0  
	g_App\Light1%	=	0  
	g_App\Plane1%	=	0  
	g_App\chrono.TChrono = Null 
	g_App\Race = False 
	g_App\fntArial50B = LoadFont("Arial",50,True,False,False) 
	g_App\fntArial15B = LoadFont("Arial",15,True,False,False)  
	
	WireFrame 0
	g_App\Cam1	=	CreateCamera	() 
	PositionEntity  g_App\Cam1,230,100,10
	CameraRange g_App\Cam1,.1,1000 
	CameraClsColor g_App\Cam1,0,0,0 
	
	
 	g_App\Light1=	CreateLight		(1) 
	RotateEntity(g_App\Light1,30,300,0) 
	
	g_App\Plane1=	CreatePlane		() 
	EntityType(g_App\Plane1,kWORLD_COL) 
	EntityColor(g_App\Plane1,10,100,10) 
	NameEntity(g_App\Plane1,"SOL"+1) 
	PositionEntity g_App\Plane1,0,-0.01,0
	
	g_App\MDL =	CreateCube()
	HideEntity(g_App\MDL) 
	ScaleMesh(g_App\MDL,1,.35,2) 
	PositionMesh(g_App\MDL,0,.5,0) 
	
	
	g_App\TEMPLATE_MISSIL= LoadMesh("model/missile1.3ds")
	ScaleMesh g_App\TEMPLATE_MISSIL,.5,.5,.5
	MoveEntity g_App\TEMPLATE_MISSIL,0,1,0
	FitMesh g_App\TEMPLATE_MISSIL, -(MeshWidth (g_App\TEMPLATE_MISSIL) / 2), -(MeshHeight (g_App\TEMPLATE_MISSIL) / 2), -(MeshDepth (g_App\TEMPLATE_MISSIL) / 2), MeshWidth (g_App\TEMPLATE_MISSIL), MeshHeight (g_App\TEMPLATE_MISSIL), MeshDepth (g_App\TEMPLATE_MISSIL)
	EntityColor g_App\TEMPLATE_MISSIL, 0,125,255
	Cel_Shade( g_App\TEMPLATE_MISSIL )
	HideEntity g_App\TEMPLATE_MISSIL
	
	Local l_cabine%=CreateCube	() 
	ScaleMesh(l_cabine,.9,.5,1) 
	PositionMesh(l_cabine,0,1,-.3) 
	AddMesh(l_cabine,g_App\MDL) 
	FreeEntity(l_cabine) 
	
 	Cel_Shade( g_App\MDL,0.05)
	
	g_App\framePeriod = 1000 / g_App\gameFPS 
	g_App\frameTime = MilliSecs () - g_App\framePeriod 	
	
    ; init countdown timer
    ; when player start race for the 3 first second
	g_App\tm1 = SetTimer(1000) ; 1sec
	g_App\tm2 = SetTimer(2000) ; 2sec
	g_App\tm3 = SetTimer(3000) ; 3sec
	
	
	Collisions kCAR_COL,kWORLD_COL,2,2 
	Collisions kCAR_COL,kPLAYER_CAR_COL,3,3 
	Collisions kCAR_COL,kWALL_COL,3,2		
	
	Collisions kPLAYER_CAR_COL,kWALL_COL,3,3
	Collisions kPLAYER_CAR_COL,kCAR_COL,3,2 
	Collisions kPLAYER_CAR_COL,kWORLD_COL,2,2 
	
	g_App\RaceData=TRaceData_Create("race01")
	
	App_CreateCircuit()   
    App_CreateWayPoint()
	
	
    App_CreateIA( "IA1",0.015);, 110,1,15, 0,90,0,0.015 )  
    App_CreateIA( "IA2",0.013);, 120,1,15, 0,90,0,0.013)  
    App_CreateIA( "IA3",0.011);, 130,1,15, 0,90,0,0.011 )  
    App_CreateIA( "IA4",0.009);, 140,1,15, 0,90,0,0.009 )  
	
    App_CreateIA( "IA5",0.007);, 115,1,25, 0,90,0,0.007 )  
    App_CreateIA( "IA6",0.005);, 125,1,25, 0,90,0,0.005 )  
    App_CreateIA( "IA7",0.003);, 135,1,25, 0,90,0,0.003 )  
	
	App_SetPositionCar("IA1",26,0,-5)
	App_SetPositionCar("IA2",25,0,-5)
	App_SetPositionCar("IA3",24,0,-5)
	App_SetPositionCar("IA4",23,0,-5)
	App_SetPositionCar("IA5",22,0,-5)
	App_SetPositionCar("IA6",21,0,-5)
	App_SetPositionCar("IA7",20,0,-5)
	
    g_App\Player = App_CreatePlayer( "you", 141,1,25, 0,90,0,0.009 )  
	
	App_SetPositionCar("you",19,0,-5)
	
	App_IA_SetCollision_Type( "IA1", kCAR_COL )
	App_IA_SetCollision_Type( "IA2", kCAR_COL )
	App_IA_SetCollision_Type( "IA3", kCAR_COL )
	App_IA_SetCollision_Type( "IA3", kCAR_COL )
	App_IA_SetCollision_Type( "IA4", kCAR_COL )
	App_IA_SetCollision_Type( "IA5", kCAR_COL )
	App_IA_SetCollision_Type( "IA6", kCAR_COL )
	App_IA_SetCollision_Type( "IA7", kCAR_COL )
	
End Function 



Function App_CreatePlayer.TCar( p_player_name$, p_PosX#, p_PosY#, p_PosZ#, p_TurnX#, p_TurnY#, p_TurnZ#, p_speed# )  
	
	Local l_Player.TCar = Null 
	
	l_Player = TCar_CreateVoiture(True) 	
	
	l_Player\speed = p_speed#
	l_Player\oldspeed = p_speed#
    l_Player\name = Upper(p_player_name$)	
	PositionEntity l_Player\mesh,p_PosX#, p_PosY#, p_PosZ#
	EntityType l_Player\mesh,kPLAYER_CAR_COL 	
	NameEntity		(l_Player\mesh,"CAR_PLAYER_"+Upper(p_player_name$)) 
	Auto_EntityBox( l_Player\mesh )
	
	TurnEntity l_Player\mesh,p_TurnX#, p_TurnY#, p_TurnZ#
	
	Return l_Player 
End Function 



Function App_CreateIA.TCar( p_name$, p_speed# = 0.01 );, p_PosX#, p_PosY#, p_PosZ#, p_TurnX#, p_TurnY#, p_TurnZ#, p_speed#, p_file_mesh$ ="" )  
	Local l_Cpu.TCar = TCar_CreateVoiture(False) 
	
    l_Cpu\speed = p_speed#
	l_Cpu\oldspeed = p_speed#
    l_Cpu\name = Upper(p_name$)
	NameEntity		(l_Cpu\mesh,"CAR_"+Upper(p_name$)) 	
	EntityColor l_Cpu\mesh,200,10,10
	
	Return l_Cpu
End Function 



Function App_IA_SetCollision_Type( p_sCarName$, p_iColType )
	Local l_car.TCar = TCar_GetByName( p_sCarName$ )
	If l_car <> Null 
		EntityType l_car\mesh,p_iColType 			
		Auto_EntityBox( l_car\mesh )			
	End If 
	
End Function 



Function App_SetPositionCar(p_CarName$,p_WayPointID,p_Relx#,p_Relz#)
	Local l_car.TCar = Null
	Local l_wp.TWayPoint = Null
	
	l_car = TCar_GetByName( p_CarName$ )
	If l_car = Null 
		RuntimeError "Function App_SetPositionCar Car name '"+p_CarName$ + "' does not exist."
	EndIf 
	
	l_wp = TWayPoint_Get.TWayPoint(p_WayPointID)
	If l_wp = Null 
		RuntimeError "Function App_SetPositionCar Waypoint ID '"+p_WayPointID + "' does not exist."
	EndIf 
	
	PositionEntity l_car\mesh, EntityX(l_wp\mesh),2, EntityZ(l_wp\mesh) 
	Local l_wpNext.TWayPoint =TWayPoint_Get.TWayPoint(p_WayPointID+1)
	If l_wpNext = Null Then l_wpNext.TWayPoint =TWayPoint_Get.TWayPoint(1)
	If l_wpNext = Null
		RuntimeError "Function App_SetPositionCar Next Waypoint ID '"+p_WayPointID + "' does not exist."
	EndIf 
	
	PointEntity l_car\mesh, l_wpNext\mesh
	MoveEntity l_car\mesh, p_Relx#,0,p_Relz#
	l_car\vPos\x = EntityX( l_car\mesh )
	l_car\vPos\y = EntityY( l_car\mesh )
	l_car\vPos\z = EntityZ( l_car\mesh )
	l_car\vAcc\x = 0
	l_car\vAcc\y = 0
	l_car\vAcc\z = 0
	l_car\vVel\x = 0
	l_car\vVel\y = 0
	l_car\vVel\z = 0
	
	l_car\wayp = l_wpNext
End Function 



Function App_CreateCircuit() 
	
	Local lz#,lx#,mi_ts#   = (kTILE_SIZE/2.0)     
	Local w.TWall = Null
	Local tilenum$=""
	Local t, x_Floor, z_Floor
	; Print them all! 
	For lz# = 0 To (g_App\RaceData\nb_row-1)
		For lx# = 0 To (g_App\RaceData\nb_col-1)
			
			; get value from tile data stored into file circuit.txt
			tilenum$ = TRaceData_GetTile$( g_App\RaceData, (g_App\RaceData\nb_row-1)-lz, lx )			
			
			If tilenum$ <> "0"
				t=CreateFloor("tile"+tilenum$+".png", -mi_ts,mi_ts,-mi_ts,mi_ts)			
				
				x_Floor = (lx*(kTILE_SIZE));+lx
				z_Floor = (lz*(kTILE_SIZE));+lz
				PositionEntity t,x_Floor,0,z_Floor
				
				If tilenum$ = "1" ; =
					w=TWall_CreateWall(-mi_ts#,0,0,mi_ts#,3,1,x_Floor,0,z_Floor-mi_ts)
					NameEntity w\mesh, "WALL["+tilenum$+"].0"
					w=TWall_CreateWall(-mi_ts#,0,0,mi_ts#,3,1,x_Floor,0,z_Floor+mi_ts)
					NameEntity w\mesh, "WALL["+tilenum$+"].1"					
					
				End If
				
				If tilenum$ = "2" ; ||
					w=TWall_CreateWall(0,0,-mi_ts#,1,3,mi_ts#,x_Floor-mi_ts,0,z_Floor)
					NameEntity w\mesh, "WALL["+tilenum$+"].0"					
					w=TWall_CreateWall(0,0,-mi_ts#,1,3,mi_ts#,x_Floor+mi_ts,0,z_Floor)
					NameEntity w\mesh, "WALL["+tilenum$+"].1"					
				End If
				
				
				If tilenum$ = "3" ;L
					w=TWall_CreateWall(0,0,-mi_ts#,1,3,mi_ts#,x_Floor-mi_ts,0,z_Floor)
					NameEntity w\mesh, "WALL["+tilenum$+"].0"					
					w=TWall_CreateWall(-mi_ts#,0,0,mi_ts#,3,1,x_Floor,0,z_Floor-mi_ts)					
					NameEntity w\mesh, "WALL["+tilenum$+"].1"					
				End If
				
				If tilenum$ = "4" 
					w=TWall_CreateWall(0,0,-mi_ts#,1,3,mi_ts#,x_Floor-mi_ts,0,z_Floor)
					NameEntity w\mesh, "WALL["+tilenum$+"].0"					
					w=TWall_CreateWall(-mi_ts#,0,0,mi_ts#,3,1,x_Floor,0,z_Floor+mi_ts)
					NameEntity w\mesh, "WALL["+tilenum$+"].1"					
				End If
				
				If tilenum$ = "5" 
					w=TWall_CreateWall(0,0,-mi_ts#,1,3,mi_ts#,x_Floor+mi_ts,0,z_Floor)
					NameEntity w\mesh, "WALL["+tilenum$+"].0"					
					w=TWall_CreateWall(-mi_ts#,0,0,mi_ts#,3,1,x_Floor,0,z_Floor+mi_ts)					
					NameEntity w\mesh, "WALL["+tilenum$+"].1"					
				End If
				
				If tilenum$ = "6" 
					w=TWall_CreateWall(0,0,-mi_ts#,1,3,mi_ts#,x_Floor+mi_ts,0,z_Floor)
					NameEntity w\mesh, "WALL["+tilenum$+"].0"					
					w=TWall_CreateWall(-mi_ts#,0,0,mi_ts#,3,1,x_Floor,0,z_Floor-mi_ts)									
					NameEntity w\mesh, "WALL["+tilenum$+"].1"					
				End If
				
			EndIf 
		Next 
	Next 
End Function 



Function App_CreateWayPoint()
    Local lx#,lz#
	Local wp,x_Floor,z_Floor
	For lz# = 0 To (g_App\RaceData\nb_row-1)
		For lx# = 0 To (g_App\RaceData\nb_col-1)
			
			; get value from tile data stored into file circuit.txt
			wp = TRaceData_GetWp( g_App\RaceData, (g_App\RaceData\nb_row-1)-lz, lx )			
			If wp > 0
				x_Floor = (lx*(kTILE_SIZE));+lx
				z_Floor = (lz*(kTILE_SIZE));+lz
				TWayPoint_CreateWayPoint( wp, x_Floor,1,z_Floor )
			EndIf 
		Next 
	Next 
	
	TWayPoint_PointByOrder() ; point direction prev wp to next wp
	
	
End Function



Function App_Pre_Update() 
	If KeyHit(15)
		g_App\UserCamMode = g_App\UserCamMode + 1
		If g_App\UserCamMode > 4 Then g_App\UserCamMode = 1
 	EndIf 
	
	If g_App\UserCamMode = 1
		Camera3rdPers( g_App\Cam1, g_App\Player\mesh,6,-4 ) 
	ElseIf g_App\UserCamMode = 2
		Camera3rdPers( g_App\Cam1, g_App\Player\mesh,20,-3 ) 
	ElseIf g_App\UserCamMode = 3
		Camera3rdPers( g_App\Cam1, g_App\Player\mesh,40,-10 ) 		
	ElseIf g_App\UserCamMode = 4
		Camera3rdPers( g_App\Cam1, g_App\Player\mesh,2,1 ) 				
	EndIf 
	
   	If g_App\Race = True  
		TCar_Update() 
		Car_Position()
		TExplosion_Update()
	End If 
End Function 



Function App_Post_Update() 
	If g_App\Race
	col_update() 
	TItem_Update()
	EndIf 
End Function 



Function App_Update2D()
	
	Color 255,255,255	
	Local l_c.TCar = Null 
	
	If g_App\Race = False	
		
	    If g_App\tm1 <> Null And TimeOut(g_App\tm1) = False 
			SetFont g_App\fntArial50B
			Color 250,0,0
			Text GraphicsWidth()/2,GraphicsHeight()/2, "Three",True,True
		EndIf 
	    If g_App\tm2 <> Null And TimeOut(g_App\tm2) =  False And g_App\tm1 = Null 
			SetFont g_App\fntArial50B
			Color 250,0,0		
			Text GraphicsWidth()/2,GraphicsHeight()/2, "Two",True,True
		EndIf 
	    If g_App\tm3 <> Null And TimeOut(g_App\tm3) = False And g_App\tm2 = Null 
			SetFont g_App\fntArial50B
			Color 250,0,0		
			Text GraphicsWidth()/2,GraphicsHeight()/2, "one",True,True
		EndIf 
		
		
		If g_App\tm3 = Null And g_App\tm2 = Null And g_App\tm1 = Null And g_App\Race = False Then 
		    g_App\Race = True
			g_App\chrono = TChrono_Create()	
			TChrono_StartStop(g_App\chrono)				   	
		EndIf 
	Else 
		SetFont g_App\fntArial15B
		Color 250,255,0
        If g_App\debug = True 
			TWayPoint_Text(g_App\Cam1)
			
			
			For l_c.TCar = Each TCar
				If EntityInView(l_c\mesh,g_App\Cam1)
					CameraProject(g_App\Cam1,EntityX(l_c\mesh),EntityY(l_c\mesh),EntityZ(l_c\mesh)) 
					Text ProjectedX#(),ProjectedY#(), EntityName(l_c\mesh)+"  Lap "+l_c\nb_loop+", Pos="+l_c\Race_pos
				EndIf
			Next 
			
	    EndIf
	    
		; display position lap
		Local t$[30]	
		Text 10,10 , "Position and Lap"
		Color 255,0,0
		For l_c.TCar = Each TCar		
			t[l_c\Race_pos] = "  Lap "+l_c\nb_loop + "   -   " + EntityName(l_c\mesh)
		Next 
		Local i=0,l=0
		For i=1 To TCar_count()-1
			If Instr(t[i], g_App\Player\name) 
				Color 255,255,255
			Else 	
				Color 255,30,30
			EndIf 
			
			Text 1,l+30, i + ". " +t[i]
			l=l+12
		Next 
		
		If g_App\Player\bMissil = True Then
			Color 105,105,105
			Rect GraphicsWidth()-200,0,200,40
			Color 0,255,255
			Text GraphicsWidth()-200,10 , "Missilie Loaded(Fire=CTRL Left)"
		EndIf 
		
		Color 255,255,255
		Text 10,GraphicsHeight()-40, "Camera View( presse Tab )"
	EndIf 
	
End Function 


Function deform_mesh(p_surf, p_fdeformFactor# = 0.01 )
	Local vertices=CountVertices(p_surf)
	Local x#,y#,z#,d#,dx#,dy#,dz#
	For i=0 To vertices-1
		x# = VertexX(p_surf,i)
		y# = VertexY(p_surf,i)
		z# = VertexZ(p_surf,i)
		
		d#= p_fdeformFactor#
		dx# = Rnd(-d,d)
		dy# = Rnd(-d,d)
		dz# = Rnd(-d,d)		
		
		VertexCoords p_surf,i,x#+dx,y#+ dy,z#+ dz
	Next
	
End Function 


Function col_update() 
	Local Collide%=False
	Local v.TCar = Null
	Local nx#,ny#,nz#
	Local nc,ent2
	Local name$
	Local norm#
	
	If g_App\Race = False Then Return  
	
	
	For v.TCar=Each TCar 
		If v\bSpeed=True 
			If TimeOut(v\WaitSpeedTime ) Then
				v\bSpeed = False
				v\WaitSpeedTime=Null
				v\speed = v\oldspeed
			EndIf 			
		EndIf
		
		If v\bExploded = False
			Collide		=	False 
			; test collisions 
			For nc=1 To CountCollisions(v\mesh) 
				nx#		=	CollisionNX(v\mesh,nc) 
				ny#		=	CollisionNY(v\mesh,nc) 
				nz#		=	CollisionNZ(v\mesh,nc) 
				ent2%	=	CollisionEntity(v\mesh,nc) 
				name$	=	EntityName(ent2) 
				
				; si voiture +> retour de force 
				If Len(name)>3
					If Left(name,3)="CAR" 
						norm#	=	Sqr(v\vVel\x*v\vVel\x+v\vVel\y*v\vVel\y+v\vVel\z*v\vVel\z)*.88 
						v\vAcc\x	=	v\vAcc\x+norm*nx 
						v\vAcc\y	=	v\vAcc\y+norm*ny 
						v\vAcc\z	=	v\vAcc\z+norm*nz 
						v\vVel\x	=	v\vVel\x*.8 
						v\vVel\y	=	v\vVel\y*.8 
						v\vVel\z	=	v\vVel\z*.8 
						Collide	=	True 
						
					ElseIf Left(name,3)="SOL" 
						; adherence +> Sol 
						v\vVel\x	=	v\vVel\x*.98 
						v\vVel\y	=	0 
						v\vVel\z	=	v\vVel\z*.98
					Else
						
						norm#	=	Sqr(v\vVel\x*v\vVel\x+v\vVel\y*v\vVel\y+v\vVel\z*v\vVel\z)*.88 
						v\vAcc\x	=	v\vAcc\x+norm*nx 
						v\vAcc\y	=	v\vAcc\y+norm*ny 
						v\vAcc\z	=	v\vAcc\z+norm*nz 
						v\vVel\x	=	v\vVel\x*.1 
						v\vVel\y	=	v\vVel\y*.1 
						v\vVel\z	=	v\vVel\z*.1 
						Collide	=	True 
					EndIf 
				EndIf 
			Next 
			
			If Collide=True 
				;correct velocity if collided 
				v\vVel\x=(EntityX(v\mesh)-v\vPos\x) 
				v\vVel\y=(EntityY(v\mesh)-v\vPos\y) 
				v\vVel\z=(EntityZ(v\mesh)-v\vPos\z) 
			EndIf
		Else 
			HideEntity( v\mesh )
			v\Oldx#=EntityX(v\mesh )
			v\Oldy#=EntityY(v\mesh )+5
			v\Oldz#=EntityZ(v\mesh )
			v\vPos = Vector_createVector( 0.0, 0.0, 0.0 )
			v\vVel = Vector_createVector( 0.0, 0.0, 0.0 )
			v\vAcc = Vector_createVector( 0.0, 0.0, 0.0 )
			If TimeOut(v\WaitExplodeTime ) Then
				ShowEntity( v\mesh )
				v\bExploded = False
				v\WaitExplodeTime=Null
				PositionEntity v\mesh,v\Oldx,v\Oldy,v\Oldz
				
				v\bSpeed = False
				v\WaitSpeedTime=Null
				v\speed#=v\oldspeed				
			EndIf 
		EndIf 
	Next 
	
	
End Function 







Function Car_Position()
    Local tmp=0
    Local tmpn$=""
 	Local l_car[50]
 	Local l_car_pos[50]
 	Local l_car_name$[50]
	
	; for each_car calculate Race_pos_factor = loop done + nearest way point
	Local j=0,i=0
	Local l_c.TCar
	For l_c.TCar = Each TCar 
		l_c\Race_pos_factor = (l_c\nb_loop * TWayPoint_Count() )+ l_c\wayp\order_sequence
		l_car[i] = l_c\mesh
		l_car_pos[i] = l_c\Race_pos_factor
		l_car_name[i] = l_c\name$
		l_c\Race_pos = 0
		i=i+1
	Next 
	
	For i=0 To TCar_count()-1
		For j=0 To TCar_count()-1 
			If i<>j Then 
				If l_car_pos[i] >  l_car_pos[j]
					tmp=l_car_pos[i]
					l_car_pos[i] = l_car_pos[j]
					l_car_pos[j] = tmp 
					
					tmp=l_car[i]
					l_car[i] = l_car[j]
					l_car[j] = tmp 				
					
					tmpn$ = l_car_name[i]
					l_car_name[i] = l_car_name[j]
					l_car_name[j] = tmpn$
				End If
			EndIf
		Next
	Next 
	
	For i=0 To TCar_count()-1
		l_c = TCar_GetByName( l_car_name[i] )
		If l_c <> Null 
			l_c\Race_pos_factor=l_car_pos[i]
			l_c\Race_pos = i
		Else 
			Stop 
		End If
	Next 	
	
	
End Function



; Structure who store all info about a spherical Line






;~IDEal Editor Parameters:
;~C#Blitz3D